package cn.johnyu.base.controller;

import cn.johnyu.base.dao.BookMapper;
import cn.johnyu.base.model.Book;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;
@RestController
public class BookController {
    @Autowired private BookMapper bookMapper;

    @GetMapping("/api/books")
    public List<Book> findAllBooks(){
        return bookMapper.findAllBooks();
    }
    @GetMapping("/api/books/{id}")
    public Book findBookById(@PathVariable("id") Integer id){
        return bookMapper.findBookById(id);
    }

    @GetMapping("/api/publishers/{id}/books")
    public List<Book> findBooksByPublisherId(@PathVariable("id") Integer id){
        return bookMapper.findBooksByPublisherId(id);
    }


    @GetMapping("/api/authors/{id}/books")
    public List<Book> findBooksByAuthorId(@PathVariable("id") Integer id){
        return bookMapper.findBooksByAuthorId(id);
    }
}
